package com.master.service.householder;

import com.master.constant.model.CsHouseholder;
import com.master.controller.householder.CsHouseholderVO;
import com.master.mapper.modelMapper.CsHouseholderDao;
import org.springframework.stereotype.Service;
import com.master.common.baseService.BaseServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * @author Heath.Que
 * @version 1.0
 * @classname
 * @description 顾客接口实现类
 * @Date 2021/3/25 9:22
 **/
@Service
public class HouseholdServiceImpl extends BaseServiceImpl implements HouseholdService {

    private final CsHouseholderDao csHouseholderDao;

    public HouseholdServiceImpl(CsHouseholderDao csHouseholderDao) {
        this.csHouseholderDao = csHouseholderDao;
    }

    /**
     * @Description: 顾客查询接口
     * @Parameter: [params]
     * @Return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author: Heath.Que
     * @Date: 2021/5/31
     **/
    @Override
    public List<Map<String, Object>> getHouseholdersByCondition(Map<String, Object> condition) {

        return csHouseholderDao.getHouseholderByCondition(condition);
    }

    @Override
    public Boolean addHouseholders(CsHouseholderVO csHouseholderVO) {
        List<CsHouseholder> householders = csHouseholderVO.getHouseholders();
        //添加结果标识
        boolean mark = false;
        try {
            if (householders.size() > 1) {//单条数据导入
                csHouseholderDao.insertSelective(householders.get(0));
            } else {//多条数据导入
                householders.forEach(csHouseholderDao::insertSelective);
            }
            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }

        return mark;
    }

    @Override
    public Boolean addHouseholdersByHouseId(CsHouseholderVO csHouseholderVO) {
        List<CsHouseholder> householders = csHouseholderVO.getHouseholders();
        //添加结果标识
        boolean mark = false;
        try {
            csHouseholderDao.addHouseholdersByHouseId(csHouseholderVO.getHouseId(), householders);
            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }

        return mark;
    }

    @Override
    public Boolean modifyHouseholders(CsHouseholderVO csHouseholderVO) {
        List<CsHouseholder> householders = csHouseholderVO.getHouseholders();
        //添加结果标识
        boolean mark = false;
        try {
            if (householders.size() > 1) {//单条数据修改
                csHouseholderDao.updateByPrimaryKeySelective(householders.get(0));
            } else {//多条数据修改
                householders.forEach(csHouseholderDao::updateByPrimaryKeySelective);
            }
            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }

        return mark;
    }

    @Override
    public Boolean deleteHouseholders(List<Long> householderIds) {
        //添加结果标识
        boolean mark = false;
        try {
            if (householderIds.size() > 1) {//单条数据删除
                csHouseholderDao.deleteByPrimaryKey(householderIds.get(0));
            } else {//多条数据删除
                householderIds.forEach(csHouseholderDao::deleteByPrimaryKey);
            }
            mark = true;
        } catch (Exception e) {
            getLogger().error(e.getMessage());
        }

        return mark;
    }


}
